package com.xindong.paking.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xindong.paking.dtos.OrderDTO;
import com.xindong.paking.entity.TbOrder;
import com.xindong.paking.entity.TbSpace;
import com.xindong.paking.entity.constant.Constants;
import com.xindong.paking.service.TbOrderService;
import com.xindong.paking.service.TbSpaceService;
import com.xindong.paking.utils.ChangeUtil;
import com.xindong.paking.utils.Result;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author jielun
 * @since 2023-01-08
 */
@RestController
@RequestMapping("/parting/tb-order")
@Api(tags = "订单管理模块")
public class TbOrderController {

    @Autowired
    private TbOrderService orderService;

    @Autowired
    private TbSpaceService spaceService;

    @Autowired
    private ChangeUtil changeUtil = new ChangeUtil();

    @GetMapping("list")
    @ApiOperation("获取所有订单")
    public Result getList() {
        List<TbOrder> list = orderService.list();
        List<OrderDTO> orderDTOList = changeUtil.changeModel(list);
        return Result.success("获取订单成功", orderDTOList);
    }

    @GetMapping("queryByUser")
    @ApiOperation("根据用户id查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "status", value = "是否是当前正在执行的订单")
    })
    public Result queryByUser(
            @RequestParam(value = "userId") int userId,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        return orderService.queryByUser(userId, status);
    }

    @GetMapping("queryBycondition")
    @ApiOperation("订单条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "carNumber", value = "车牌号"),
            @ApiImplicitParam(name = "status", value = "订单状态"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "page", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    public Result queryBycondition(
            @RequestParam(value = "userId", required = false) String userId,
            @RequestParam(value = "carNumber", required = false) String carNumber,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) throws ParseException {
        if (page == null) page = 0;
        if (pageSize == null) pageSize = 100;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTimeDate = null;
        Date endTimeDate = null;
        if (startTime != null) {
            startTimeDate = sdf.parse(startTime);
        }
        if (endTime != null) {
            endTimeDate = sdf.parse(endTime);
        }
        return orderService.queryBycondition(userId, carNumber, status, startTimeDate, endTimeDate, page, pageSize);
    }

    @PostMapping("addOrder")
    @ApiOperation("用户添加订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "carNumber", value = "车牌号"),
            @ApiImplicitParam(name = "parkingId", value = "停车场id"),
            @ApiImplicitParam(name = "positionId", value = "停车位id")
    })
    public Result addOrder(
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "carNumber", required = false) String carNumber,
            @RequestParam(value = "parkingId", required = false) Integer parkingId,
            @RequestParam(value = "positionId", required = false) Integer positionId
    ) {
        TbOrder tbOrder = orderService.addOrder(userId, carNumber, parkingId, positionId);
        return Result.success(Constants.DEFAULT_INSERT_SUCCESS_MSG,tbOrder);
    }

    @GetMapping("finishOrder")
    @ApiOperation("结束订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id"),
    })
    public Result finishOrder(
            @RequestParam(value = "orderId", required = false) Integer orderId
    ) {
        return orderService.finishOrder(orderId);
    }

    @GetMapping("getOrderById")
    @ApiOperation("根据id获取订单详情")
    public Result getOrderById(@RequestParam("orderid") Integer orderid) {
        return orderService.getOrderById(orderid);
    }

    @GetMapping("payForOrder")
    @ApiOperation("支付订单")
    public Result payForOrder(@RequestParam("orderid") Integer orderid) {
        return orderService.payForOrder(orderid);
    }


    @GetMapping("totalMoney")
    @ApiOperation("获取十二个月月度总金额")
    public Result totalMoney() {
        return orderService.getOrderMoney();
    }

    @GetMapping("totalOrder")
    @ApiOperation("获取十二个月每月总订单数")
    public Result totalOrder() {
        return orderService.totalOrder();
    }
}

